import 'package:get/get.dart';
import 'package:safetymonitor/app/components/toast_widget.dart';
import 'package:safetymonitor/app/core/http/http.dart';
import 'package:safetymonitor/app/core/utils/interface.dart';
import 'package:safetymonitor/app/data/entities/home/warning/alarm_resp_entity.dart';
import 'package:safetymonitor/modules/home/warning_disposal/warning_disposal_controller.dart';

class DisposalDetailsController extends GetxController {
  /// 实体数据
  AlarmRespEntity entity = Get.arguments as AlarmRespEntity;

  /// 列表控制器
  final listController = Get.find<WarningDisposalController>();

  ///	处置附件列表
  List<String> disposalMeasuresAnnex = [];

  /// 销警描述附件
  List<String> disarmingDescriptionAnnex = [];

  /// 当前页面类型
  /// 1处置  2消警 3处置完成
  late int type;

  @override
  void onInit() {
    super.onInit();
    //[entity.status] 1未处置  2处置中 3处置完成
    type = entity.status;
    // 设置回调附件
    if (entity.disposalMeasuresAnnex.isNotEmpty) {
      disposalMeasuresAnnex = entity.disposalMeasuresAnnex.split(',');
    }
    if (entity.disarmingDescriptionAnnex.isNotEmpty) {
      disarmingDescriptionAnnex = entity.disarmingDescriptionAnnex.split(',');
    }
  }

  /// 确定提交
  Future<void> confirm() async {
    // 处置描述
    if (type == 1 && entity.disposalMeasures.isEmpty) {
      AppToast.showError('请输入处置措施');
      return;
    }

    // 消警描述
    if (type == 2 && entity.disarmingDescription.isEmpty) {
      AppToast.showError('请输入消警描述');
      return;
    }

    // 附件信息
    String? files;
    if (type == 1 && disposalMeasuresAnnex.isNotEmpty) {
      files = disposalMeasuresAnnex.join(',');
    } else if (type == 2 && disarmingDescriptionAnnex.isNotEmpty) {
      files = disarmingDescriptionAnnex.join(',');
    }

    final res = await HttpUtil().generatePutRequest(
      Interface.putDisposeAlarm,
      data: {
        'id': entity.id,
        'type': type,
        'description': type == 1 ? entity.disposalMeasures : entity.disarmingDescription,
        'disarmingDescriptionAnnex': files,
      },
    ).execute(isShowLoading: true);

    res.onSuccess((result) {
      Get.back(result: 'success');
      AppToast.showSuccess('处置成功!');
    });
  }
}
